package com.tothefor.controller;

import com.tothefor.pojo.dto.AnswererDTO;
import com.tothefor.pojo.dto.QuestionnaireResultDTO;
import com.tothefor.pojo.entity.Question;
import com.tothefor.pojo.entity.QuestionnaireResult;
import com.tothefor.resultR.R;
import com.tothefor.service.impl.QuestionServiceImpl;
import com.tothefor.service.impl.QuestionnaireResultServiceImpl;
import com.tothefor.service.impl.QuestionnaireServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/questionnaireResult")
public class QuestionnaireResultController {
    @Autowired
    private QuestionnaireServiceImpl QuestionnaireService;

    @Autowired
    private QuestionServiceImpl QuestionService;

    @Autowired
    private QuestionnaireResultServiceImpl QuestionnaireResultService;



    @PostMapping("/submitQuestionnaire")
    public R<?> submitQuestionnaire(@RequestBody QuestionnaireResultDTO resultDTO) {
        List<Object> answerList1 = resultDTO.getAnswerList1();
        List<Object> answerList2 = resultDTO.getAnswerList2();
        List<Object> answerList3 = resultDTO.getAnswerList3();
        List<Object> questionID = resultDTO.getQuestionID();
        List<Question> questions = QuestionService.list();
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int p = 0;
        for (Question question : questions) {
            QuestionnaireResult QuestionnaireResult = new QuestionnaireResult();
            for (Object i : questionID) {
                if (i.equals(question.getId())) {
                    if (question.getType().equals("1")) {
                        if(answerList1.get(count1).equals("1")){
                            QuestionnaireResult.setAnswer1(1);
                            QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));
                            QuestionnaireResultService.save(QuestionnaireResult);
                            count1++;
                        }else if(answerList1.get(count1).equals("2")){
                            QuestionnaireResult.setAnswer2(1);
                            QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));
                            QuestionnaireResultService.save(QuestionnaireResult);
                            count1++;
                        }else if(answerList1.get(count1).equals("3")){
                            QuestionnaireResult.setAnswer3(1);
                            QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));
                            QuestionnaireResultService.save(QuestionnaireResult);
                            count1++;
                        }
                        else if(answerList1.get(count1).equals("4")){
                            QuestionnaireResult.setAnswer4(1);
                            QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));
                            QuestionnaireResultService.save(QuestionnaireResult);
                            count1++;
                        }

                    } else if (question.getType().equals("2")) {
                        List<Object> thisAnswerList = (List<Object>) answerList2.get(count2);
                       for(p= 0;p<thisAnswerList.size();p++){
                          if(thisAnswerList.get(p).equals("1")){
                               QuestionnaireResult.setAnswer1(1);
                               QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));


                           }if(thisAnswerList.get(p).equals("2")){
                               QuestionnaireResult.setAnswer2(1);
                               QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));


                           }if(thisAnswerList.get(p).equals("3")){
                               QuestionnaireResult.setAnswer3(1);
                               QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));


                           }if(thisAnswerList.get(p).equals("4")){
                               QuestionnaireResult.setAnswer4(1);
                               QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));

                           }


                        }
                        count2++;
                        QuestionnaireResultService.save(QuestionnaireResult);
                    } else if (question.getType().equals("3")) {
                        QuestionnaireResult.setAnswertext((String) answerList3.get(count3));
                        QuestionnaireResult.setQuestionId(String.valueOf(question.getId()));
                        QuestionnaireResultService.save(QuestionnaireResult);
                        count3++;

                    }
                }

            }
        }
        return R.SUCCESS();
    }

    @PostMapping("/update")
    public R<?> update(@RequestBody AnswererDTO answererDTO){
        List<QuestionnaireResult> questionnaireResults = QuestionnaireResultService.list();
        int questionnaireID = answererDTO.getQuestionnaireId();
        String answererName = answererDTO.getAnswererId();
        for(QuestionnaireResult result:questionnaireResults){
            if(result.getQuestionnaireId()==null){
                result.setQuestionnaireId(String.valueOf(questionnaireID));
                result.setAnswererName(answererName);
                QuestionnaireResultService.updateById(result);
            }


        }
        return R.SUCCESS(answererDTO);
    }
}
